package com.android.videotest.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.FloatProperty;
import android.util.Log;
import android.util.Property;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;

import com.android.videotest.R;

public class TranslateActivity extends AppCompatActivity {
    public static void start(Context context) {
        Intent starter = new Intent(context, TranslateActivity.class);
//    starter.putExtra();
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_translate);

        View origin = findViewById(R.id.origin);
        View target = findViewById(R.id.target);
        View btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float x = target.getX();
                float y = target.getY();
                Log.d(this.getClass().getName(), "onClick: " + x + " " + y);
//                origin.setX(x);
//                origin.setY(y);

//                PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat(new Property<View, Float>(Float.class, "name1") {
//                    @Override
//                    public void set(View object, Float value) {
//                        Log.d(this.getClass().getName(), "set: " + value);
//                        ViewGroup.LayoutParams layoutParams = object.getLayoutParams();
//                        layoutParams.width = value.intValue();
//                        object.setLayoutParams(layoutParams);
//                    }
//
//                    @Override
//                    public Float get(View view) {
//                        return (float) view.getWidth();
//                    }
//
//                }, origin.getWidth(), target.getWidth());
                scaleTranslateTarget(origin, target);
            }
        });


    }

    @SuppressLint("ObjectAnimatorBinding")
    private void scaleTranslateTarget(View origin,View target) {
        new ValueAnimator();
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("X", origin.getX(), target.getX());
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("Y", origin.getY(), target.getY());
        PropertyValuesHolder holder3 = PropertyValuesHolder.ofInt("width", origin.getWidth(), target.getWidth());
        PropertyValuesHolder holder4 = PropertyValuesHolder.ofInt("height", origin.getHeight(), target.getHeight());
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(origin, holder1, holder2, holder3, holder4);
        objectAnimator.setDuration(800);
        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int width = (int) animation.getAnimatedValue("width");
                int height = (int) animation.getAnimatedValue("height");
                ViewGroup.LayoutParams layoutParams = origin.getLayoutParams();
                layoutParams.width = width;
                layoutParams.height = height;
                origin.setLayoutParams(layoutParams);
            }
        });
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.start();
    }

    @SuppressLint("ObjectAnimatorBinding")
    private void zoomOutTarget(View origin,View target) {
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("X", origin.getX(), target.getX());
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("Y", origin.getY(), target.getY());
        PropertyValuesHolder holder3 = PropertyValuesHolder.ofInt("width", origin.getWidth(), target.getWidth());
        PropertyValuesHolder holder4 = PropertyValuesHolder.ofInt("height", origin.getHeight(), target.getHeight());
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(origin, holder1, holder2, holder3, holder4);
        objectAnimator.setDuration(1000);
        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int width = (int) animation.getAnimatedValue("width");
                int height = (int) animation.getAnimatedValue("height");
                ViewGroup.LayoutParams layoutParams = origin.getLayoutParams();
                layoutParams.width = width;
                layoutParams.height = height;
                origin.setLayoutParams(layoutParams);
            }
        });
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.start();
    }

//    PropertyValuesHolder holder3 = PropertyValuesHolder.ofMultiFloat(new Property<View, Float>(Float.class, "name1") {
//        @Override
//        public void set(View object, Float value) {
//            Log.d(this.getClass().getName(), "set: " + value);
//            ViewGroup.LayoutParams layoutParams = object.getLayoutParams();
//            layoutParams.width = value.intValue();
//            object.setLayoutParams(layoutParams);
//        }
//
//        @Override
//        public Float get(View view) {
//            return (float) view.getWidth();
//        }
//
//    }, origin.getWidth(), target.getWidth());
}
